package com.tpc.client.modules.core.contact.datasource;

import com.tpc.dao.extend.Contact;
import com.tpc.client.modules.core.contact.service.ContactService;
import com.tpc.client.modules.core.contact.service.ContactServiceAsync;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.smartgwt.client.data.Criteria;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.data.DSResponse;
import com.tpc.client.systeme.core.datasource.GwtRpcDataSource;
import com.smartgwt.client.data.DataSourceField;
import com.smartgwt.client.rpc.RPCResponse;
import com.smartgwt.client.types.FieldType;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.util.Page;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.validator.MaskValidator;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.tpc.client.systeme.core.Systeme;

public class ContactFormDS extends GwtRpcDataSource {

    String strurl;
    ListGrid RELOAD_GRID;
    Systeme systeme;
    DynamicForm FORMEDIT;

    public ContactFormDS(String strID) {
            setID(strID);
            DataSourceField fldIdContact = new DataSourceField("id_contact", FieldType.INTEGER);
            fldIdContact.setPrimaryKey(Boolean.TRUE);
            fldIdContact.setHidden(Boolean.TRUE);
            DataSourceField fldLastName_contact = new DataSourceField("lastname_contact", FieldType.TEXT, "Last Name",295);
            fldLastName_contact.setRequired(Boolean.FALSE);
            DataSourceField fldFirstName_contact = new DataSourceField("firstname_contact", FieldType.TEXT, "First Name",175);
            fldFirstName_contact.setRequired(Boolean.TRUE);
            DataSourceField fldUsername_contact = new DataSourceField("contact_form_username", FieldType.TEXT, "Nom d'usager");
            DataSourceField fldPassword_contact = new DataSourceField("contact_form_password", FieldType.TEXT, "Mot de passe");
            DataSourceField fldTitle_contact = new DataSourceField("title_contact", FieldType.TEXT, "Title",115);
            DataSourceField fldOrganisation_contact = new DataSourceField("organisation_contact", FieldType.TEXT, "Organization",340);
            fldOrganisation_contact.setRequired(Boolean.TRUE);
            DataSourceField fldPhoneBusiness_contact = new DataSourceField("phonebusiness_contact", FieldType.TEXT, "Business Phone",115);
                MaskValidator maskValidator = new MaskValidator();
                maskValidator.setMask("^\\s*(1?)\\s*\\(?\\s*(\\d{3})\\s*\\)?\\s*-?\\s*(\\d{3})\\s*-?\\s*(\\d{4})\\s*$");
            fldPhoneBusiness_contact.setValidators(maskValidator);
            DataSourceField fldPhoneBusinessExtension_contact = new DataSourceField("phonebusinessextension_contact", FieldType.TEXT, "Ext.",115);
            DataSourceField fldPhoneHome_contact = new DataSourceField("phonehome_contact", FieldType.TEXT, "Residential Phone",115);
            fldPhoneHome_contact.setValidators(maskValidator);
            DataSourceField fldPhoneMobile_contact = new DataSourceField("phonemobile_contact", FieldType.TEXT, "Mobile Phone",115);
            fldPhoneMobile_contact.setValidators(maskValidator);
            DataSourceField fldOrganisationAddress_contact = new DataSourceField("organisationAddressDisplay", FieldType.TEXT, "",340);
            DataSourceField fldEmail1_contact = new DataSourceField("email1_contact", FieldType.TEXT, "Email 1");
            DataSourceField fldEmail2_contact = new DataSourceField("email2_contact", FieldType.TEXT, "Email 2");
            DataSourceField fldLanguage_contact = new DataSourceField("language_contact", FieldType.TEXT, "Language",115);
            DataSourceField fldFunction_contact = new DataSourceField("function_contact", FieldType.TEXT, "Function",150);
            DataSourceField fldFunctionSince_contact = new DataSourceField("functionsince_contact", FieldType.TEXT, "Function Since",120);
            DataSourceField fldBirthay_contact = new DataSourceField("birthday_contact", FieldType.DATE, "Birthday",120);
            DataSourceField fldHotIssues_contact = new DataSourceField("hotissues_contact", FieldType.TEXT, "Hot Issues",120);
            DataSourceField fldNotes_contact = new DataSourceField("notes_contact", FieldType.TEXT, "Notes");
            DataSourceField fldContact_AddressHomeId = new DataSourceField("addressId", FieldType.INTEGER);
            fldContact_AddressHomeId.setHidden(Boolean.TRUE);
            DataSourceField fldContact_AddressHomeStreet = new DataSourceField("street", FieldType.TEXT,"Rue");
            DataSourceField fldContact_AddressHomeZipcodeId = new DataSourceField("zipcode_oldId", FieldType.INTEGER);
            fldContact_AddressHomeZipcodeId.setHidden(Boolean.TRUE);
            DataSourceField fldContact_AddressHomeZipCode = new DataSourceField("zipcode", FieldType.TEXT,"Code Postal");
            DataSourceField fldContact_AddressHomeCity = new DataSourceField("city", FieldType.TEXT,"Ville");
            DataSourceField fldContact_AddressHomeState = new DataSourceField("state", FieldType.TEXT,"Province");
            DataSourceField fldContact_AddressHomeCountry = new DataSourceField("country", FieldType.TEXT,"Pays");

            DataSourceField fldOrganisationId_contact = new DataSourceField("organisationId_contact", FieldType.INTEGER);
            fldOrganisationId_contact.setHidden(Boolean.TRUE);
            DataSourceField fldCreatedDate_contact = new DataSourceField("createddate_contact", FieldType.TEXT);
            DataSourceField fldModifiedDate_contact = new DataSourceField("modifieddate_contact", FieldType.TEXT);
            DataSourceField fldChangeHandler = new DataSourceField("changedHandler_ctc",FieldType.TEXT);
            fldChangeHandler.setHidden(Boolean.TRUE);
            DataSourceField fldSaveOnExit = new DataSourceField("saveOnExit",FieldType.TEXT);
            fldSaveOnExit.setHidden(Boolean.TRUE);

            setFields(fldLastName_contact,fldFirstName_contact,fldUsername_contact,fldPassword_contact,fldTitle_contact,fldOrganisation_contact,fldPhoneBusiness_contact,fldPhoneBusinessExtension_contact,fldPhoneHome_contact,fldPhoneMobile_contact,fldOrganisationAddress_contact,fldEmail1_contact,fldEmail2_contact,fldLanguage_contact,fldFunction_contact,fldFunctionSince_contact,fldBirthay_contact,fldHotIssues_contact,fldNotes_contact,fldContact_AddressHomeId,fldContact_AddressHomeStreet,fldContact_AddressHomeZipcodeId,fldContact_AddressHomeZipCode,fldContact_AddressHomeCity,fldContact_AddressHomeState,fldContact_AddressHomeCountry,fldCreatedDate_contact,fldModifiedDate_contact,fldIdContact,fldOrganisationId_contact,fldChangeHandler,fldSaveOnExit);

            strurl = Page.getAppDir();
            strurl = strurl.substring(0,strurl.length()-1);
            strurl = strurl.substring(strurl.lastIndexOf("/"));
    }

    public void setObjects(ListGrid gridparam,DynamicForm formParam,Systeme cacheParam) {
        RELOAD_GRID = gridparam;
        systeme = cacheParam;
        FORMEDIT = formParam;
    }

    @Override
    protected void executeFetch (final String requestId, final DSRequest request, final DSResponse response) {
       Criteria criteria = request.getCriteria();
       String strId = "-1";
       if (criteria != null) {
           strId = criteria.getAttribute("id_contact");
       }
       ContactServiceAsync service = GWT.create(ContactService.class);
        ServiceDefTarget endpoint = (ServiceDefTarget) service;
        endpoint.setServiceEntryPoint(strurl+"/com.tpc.contact/contactloader");
        service.load(strId,new AsyncCallback<Contact> () {
            public void onFailure (Throwable caught) {
                systeme.stopLoading();
                response.setStatus (RPCResponse.STATUS_FAILURE);
                processResponse (requestId, response);
            }
            public void onSuccess (Contact result) {
                ListGridRecord[] list = null;
                if (result != null) {
                    list = new ListGridRecord[1];
                    list[0] = new ListGridRecord();
                    list[0].setAttribute("id_contact", result.getId());
                    list[0].setAttribute("lastname_contact",result.getLastName());
                    list[0].setAttribute("firstname_contact",result.getFirstName());
                    list[0].setAttribute("contact_form_username",result.getUsername());
                    list[0].setAttribute("contact_form_password",result.getPassword());
                    list[0].setAttribute("title_contact", result.getTitle().getId());
                    if (result.getOrganisation() != null) {
                        list[0].setAttribute("organisation_contact", result.getOrganisation().getName());
                        list[0].setAttribute("organisationId_contact", result.getOrganisation().getId());
                    }
                    if (result.getPhones("BUSINESS") != null) {
                        list[0].setAttribute("phonebusinessId_contact",result.getPhones("BUSINESS").getId());
                        list[0].setAttribute("phonebusiness_contact",result.getPhones("BUSINESS").getAreacode()+result.getPhones("BUSINESS").getNumber());
                        list[0].setAttribute("phonebusinessextension_contact", result.getPhones("BUSINESS").getExtension());
                    }
                    if (result.getPhones("MOBILE") != null) {
                        list[0].setAttribute("phonemobileId_contact",result.getPhones("MOBILE").getId());
                        list[0].setAttribute("phonemobile_contact",result.getPhones("MOBILE").getAreacode()+result.getPhones("MOBILE").getNumber());
                    }
                    if (result.getPhones("HOME") != null) {
                        list[0].setAttribute("phonehomeId_contact",result.getPhones("HOME").getId());
                        list[0].setAttribute("phonehome_contact",result.getPhones("HOME").getAreacode()+result.getPhones("HOME").getNumber());
                    }

                    if (result.getEmail() != null) {
                        list[0].setAttribute("email1_contact", result.getEmail().getUrl());
                        list[0].setAttribute("email1Id_contact", result.getEmail().getId());
                    }
                    if (result.getEmail2() != null) {
                        list[0].setAttribute("email2_contact", result.getEmail2().getUrl());
                        list[0].setAttribute("email2Id_contact", result.getEmail2().getId());
                    }
                    if (result.getLangue() != null) {
                        if (result.getLangue().getId() > 0) {
                            list[0].setAttribute("language_contact", result.getLangue().getId());
                        }
                    }
                    if (result.getAddressHome() != null) {
                        list[0].setAttribute("addressId",result.getAddressHome().getId());
                        list[0].setAttribute("street",result.getAddressHome().getStreet()+" "+result.getAddressHome().getSuite());
                        list[0].setAttribute("zipcode_oldId",result.getAddressHome().getZipcodeId());
                        list[0].setAttribute("zipcode",result.getAddressHome().getZipcode());
                        list[0].setAttribute("city",result.getAddressHome().getCityName());
                        list[0].setAttribute("state",result.getAddressHome().getStateName());
                        list[0].setAttribute("country",result.getAddressHome().getCountryName());
                    }
                    list[0].setAttribute("birthday_contact", result.getBirthDate());
                    list[0].setAttribute("notes_contact", result.getDescription());
                    list[0].setAttribute("createddate_contact",result.getModifiedDateAsString());
                    list[0].setAttribute("modifieddate_contact", result.getModifiedDateAsString());
                    list[0].setAttribute("changedHandler_ctc","false");
                    list[0].setAttribute("saveOnExit","none");
                }

                response.setData(list);
                processResponse(requestId, response);
                systeme.stopLoading();
            }
        });
    }

    @Override
    protected void executeAdd (final String requestId, final DSRequest request, final DSResponse response) {
        Boolean blnValidated = true;
        JavaScriptObject data = request.getData();
        Record dataToSave = new Record (data);
        Contact objContactToUpdate = new Contact();

        objContactToUpdate.setFirstName(dataToSave.getAttribute("firstname_contact"));
        objContactToUpdate.setLastName(dataToSave.getAttribute("lastname_contact"));
        objContactToUpdate.setUsername(dataToSave.getAttribute("contact_form_username"));
        objContactToUpdate.setPassword(dataToSave.getAttribute("contact_form_password"));
        objContactToUpdate.setTitle(0);
        if (dataToSave.getAttribute("title_contact") != null) {
            objContactToUpdate.setTitle(new Integer(dataToSave.getAttribute("title_contact")).intValue());
        }

        int zipcodeId= -1;
        if (dataToSave.getAttribute("zipcode_oldId") != null) {
            zipcodeId = new Integer(dataToSave.getAttribute("zipcode_oldId")).intValue();
        }
        objContactToUpdate.setAddressHome(-1,dataToSave.getAttribute("street"),null,zipcodeId);

        if (dataToSave.getAttribute("organisationId_contact") != null) {
            int organisationId = new Integer(dataToSave.getAttribute("organisationId_contact")).intValue();
            if (organisationId > 0) {
                objContactToUpdate.setOrganisation(organisationId);
            }
            else { blnValidated = false; }
        }
        else { blnValidated = false; }

        String strFullPhoneBusinessNumber = dataToSave.getAttribute("phonebusiness_contact");
        String strPhoneBusinessExtension = dataToSave.getAttribute("phonebusinessextension_contact");
        String strPhoneBusinessAreacode = "";
        String strPhoneBusinessNumber = "";
        if (strFullPhoneBusinessNumber != null) {
            if (strFullPhoneBusinessNumber.length() > 2) {
                strPhoneBusinessAreacode = strFullPhoneBusinessNumber.substring(0,3);
            }
            if (strFullPhoneBusinessNumber.length() > 9) {
                strPhoneBusinessNumber = strFullPhoneBusinessNumber.substring(3,10);
            }
        }
        objContactToUpdate.setPhoneBusiness(-1,strPhoneBusinessAreacode,strPhoneBusinessNumber,strPhoneBusinessExtension);
        String strFullPhoneHomeNumber = dataToSave.getAttribute("phonehome_contact");
        String strPhoneHomeAreacode = "";
        String strPhoneHomeNumber = "";
        if (strFullPhoneHomeNumber != null) {
            if (strFullPhoneHomeNumber.length() > 2) {
                strPhoneHomeAreacode = strFullPhoneHomeNumber.substring(0,3);
            }
            if (strFullPhoneHomeNumber.length() > 9) {
                strPhoneHomeNumber = strFullPhoneHomeNumber.substring(3,10);
            }
        }
        objContactToUpdate.setPhoneHome(-1,strPhoneHomeAreacode,strPhoneHomeNumber);
        String strFullPhoneMobileNumber = dataToSave.getAttribute("phonemobile_contact");
        String strPhoneMobileAreacode = "";
        String strPhoneMobileNumber = "";
        if (strFullPhoneMobileNumber != null) {
            if (strFullPhoneMobileNumber.length() > 2) {
                strPhoneMobileAreacode = strFullPhoneMobileNumber.substring(0,3);
            }
            if (strFullPhoneMobileNumber.length() > 9) {
                strPhoneMobileNumber = strFullPhoneMobileNumber.substring(3,10);
            }
        }
        objContactToUpdate.setPhoneMobile(-1,strPhoneMobileAreacode,strPhoneMobileNumber);
        objContactToUpdate.setEmail(-1,dataToSave.getAttribute("email1_contact"));
        objContactToUpdate.setEmail2(-1,dataToSave.getAttribute("email2_contact"));
        objContactToUpdate.setLangue(0);
        if (dataToSave.getAttribute("language_contact") != null) {
            objContactToUpdate.setLangue(new Integer(dataToSave.getAttribute("language_contact")).intValue());
        }
        objContactToUpdate.setFunction(0);
        if (dataToSave.getAttribute("function_contact")!= null) {
            objContactToUpdate.setFunction(new Integer(dataToSave.getAttribute("function_contact")).intValue());
        }

        objContactToUpdate.setBirthDate(dataToSave.getAttributeAsDate("birthday_contact"));

        objContactToUpdate.setDescription(dataToSave.getAttribute("notes_contact"));

        if (blnValidated) {
            ContactServiceAsync service = GWT.create (ContactService.class);
            ServiceDefTarget endpoint = (ServiceDefTarget) service;
            endpoint.setServiceEntryPoint(strurl+"/com.tpc.contact/contactloader");
            service.create(objContactToUpdate, new AsyncCallback<Contact> () {
                public void onFailure (Throwable caught) {
                    systeme.stopLoading();
                    response.setStatus (RPCResponse.STATUS_FAILURE);
                    processResponse (requestId, response);
                }
                public void onSuccess (Contact result) {
                    ListGridRecord[] list = new ListGridRecord[1];
                    list = new ListGridRecord[1];
                    
                    response.setData(list);
                    processResponse(requestId, response);
                    FORMEDIT.getItem("changedHandler_ctc").setValue("false");
                    String organisationId = "-1";
                    if (systeme.getOrganisationUI().getForm().getItem("organisation_form_id") != null) {
                        if (systeme.getOrganisationUI().getForm().getItem("organisation_form_id").getValue() != null) {
                            try {
                                organisationId = ((Integer)systeme.getOrganisationUI().getForm().getItem("organisation_form_id").getValue()).toString();
                            }
                            catch(Exception e) {
                                organisationId = (String)systeme.getOrganisationUI().getForm().getItem("organisation_form_id").getValue();
                            }
                        }
                    }
                    //systeme.getOrganisationUI().getContactMiniListe().invalidateCache();
                    //systeme.getOrganisationUI().getContactMiniListe().fetchData(new Criteria("organisationId",organisationId));
                    systeme.getAppBar().selectTab("ORGANISATIONS");
                    SC.say("Item sauvegardé !");
                }
            });
        }
        else {
            systeme.stopLoading();
            SC.say("Champs obligatoire non complété en ajout!");
        }
    }

    @Override
    protected void executeUpdate (final String requestId, final DSRequest request, final DSResponse response) {
        Boolean blnValidated = true;
        JavaScriptObject data = request.getData();
        Record dataToSave = new Record (data);
        Contact objContactToUpdate = new Contact();

        objContactToUpdate.setId(new Integer(dataToSave.getAttribute("id_contact")).intValue());

        objContactToUpdate.setFirstName(dataToSave.getAttribute("firstname_contact"));
        objContactToUpdate.setLastName(dataToSave.getAttribute("lastname_contact"));
        objContactToUpdate.setUsername(dataToSave.getAttribute("contact_form_username"));
        objContactToUpdate.setPassword(dataToSave.getAttribute("contact_form_password"));
        objContactToUpdate.setTitle(0);
        if (dataToSave.getAttribute("title_contact") != null) {
            objContactToUpdate.setTitle(new Integer(dataToSave.getAttribute("title_contact")).intValue(),FORMEDIT.getItem("title_contact").getDisplayValue());
        }

        int zipcodeId= -1;
        if (dataToSave.getAttribute("zipcode_oldId") != null) {
            zipcodeId = new Integer(dataToSave.getAttribute("zipcode_oldId")).intValue();
        }
        objContactToUpdate.setAddressHome(-1,dataToSave.getAttribute("street"),null,zipcodeId);

        if (dataToSave.getAttribute("organisationId_contact") != null) {
            int organisationId = new Integer(dataToSave.getAttribute("organisationId_contact")).intValue();
            if (organisationId > 0) {
                objContactToUpdate.setOrganisation(organisationId,dataToSave.getAttribute("organisation_contact"));
            }
            else { blnValidated = false; }
        }
        else { blnValidated = false; }

        String strPhoneBusinessId = dataToSave.getAttribute("phonebusinessId_contact");

        String strPhoneBusinessExtension = dataToSave.getAttribute("phonebusinessextension_contact");
        int intPhoneBusinessId = -1;
        if (strPhoneBusinessId != null) {
            intPhoneBusinessId = new Integer(strPhoneBusinessId).intValue();
        }
        String strFullPhoneBusinessNumber = dataToSave.getAttribute("phonebusiness_contact");
        String strPhoneBusinessAreacode = "";
        String strPhoneBusinessNumber = "";
        if (strFullPhoneBusinessNumber != null) {
            if (strFullPhoneBusinessNumber.length() > 2) {
                strPhoneBusinessAreacode = strFullPhoneBusinessNumber.substring(0,3);
            }
            if (strFullPhoneBusinessNumber.length() > 9) {
                strPhoneBusinessNumber = strFullPhoneBusinessNumber.substring(3,10);
            }
        }
        objContactToUpdate.setPhoneBusiness(intPhoneBusinessId,strPhoneBusinessAreacode,strPhoneBusinessNumber,strPhoneBusinessExtension);

        String strPhoneHomeId = dataToSave.getAttribute("phonehomeId_contact");
        int intPhoneHomeId = -1;
        if (strPhoneHomeId != null) {
            intPhoneHomeId = new Integer(strPhoneHomeId).intValue();
        }
        String strFullPhoneHomeNumber = dataToSave.getAttribute("phonehome_contact");
        String strPhoneHomeAreacode = "";
        String strPhoneHomeNumber = "";
        if (strFullPhoneHomeNumber != null) {
            if (strFullPhoneHomeNumber.length() > 2) {
                strPhoneHomeAreacode = strFullPhoneHomeNumber.substring(0,3);
            }
            if (strFullPhoneHomeNumber.length() > 9) {
                strPhoneHomeNumber = strFullPhoneHomeNumber.substring(3,10);
            }
        }
        objContactToUpdate.setPhoneHome(intPhoneHomeId,strPhoneHomeAreacode,strPhoneHomeNumber);

        String strPhoneMobileId = dataToSave.getAttribute("phonemobileId_contact");
        int intPhoneMobileId = -1;
        if (strPhoneMobileId != null) {
            intPhoneMobileId = new Integer(strPhoneMobileId).intValue();
        }
        String strFullPhoneMobileNumber = dataToSave.getAttribute("phonemobile_contact");
        String strPhoneMobileAreacode = "";
        String strPhoneMobileNumber = "";
        if (strFullPhoneMobileNumber != null) {
            if (strFullPhoneMobileNumber.length() > 2) {
                strPhoneMobileAreacode = strFullPhoneMobileNumber.substring(0,3);
            }
            if (strFullPhoneMobileNumber.length() > 9) {
                strPhoneMobileNumber = strFullPhoneMobileNumber.substring(3,10);
            }
        }
        objContactToUpdate.setPhoneMobile(intPhoneMobileId,strPhoneMobileAreacode,strPhoneMobileNumber);

        String strEmail1 = dataToSave.getAttribute("email1_contact");
        int intEmail1Id = -1;
        if (dataToSave.getAttribute("email1Id_contact") != null) {
            intEmail1Id = new Integer(dataToSave.getAttribute("email1Id_contact")).intValue();
        }
        objContactToUpdate.setEmail(intEmail1Id,strEmail1);

        String strEmail2 = dataToSave.getAttribute("email2_contact");
        int intEmailId2 = -1;
        if (dataToSave.getAttribute("email2Id_contact") != null) {
            intEmailId2 = new Integer(dataToSave.getAttribute("email2Id_contact")).intValue();
        }
        objContactToUpdate.setEmail2(intEmailId2,strEmail2);
        objContactToUpdate.setLangue(0);
        if (dataToSave.getAttribute("language_contact") != null) {
            objContactToUpdate.setLangue(new Integer(dataToSave.getAttribute("language_contact")).intValue());
        }
        objContactToUpdate.setFunction(0);
        if (dataToSave.getAttribute("function_contact") != null) {
            objContactToUpdate.setFunction(new Integer(dataToSave.getAttribute("function_contact")).intValue(),FORMEDIT.getItem("function_contact").getDisplayValue());
        }

        objContactToUpdate.setBirthDate(dataToSave.getAttributeAsDate("birthday_contact"));

        String strDescription = dataToSave.getAttribute("notes_contact");
        objContactToUpdate.setDescription(strDescription);

        if (blnValidated) {
            ContactServiceAsync service = GWT.create (ContactService.class);
            ServiceDefTarget endpoint = (ServiceDefTarget) service;
            endpoint.setServiceEntryPoint(strurl+"/com.tpc.contact/contactloader");
            service.modify(objContactToUpdate, new AsyncCallback<Contact> () {
                public void onFailure (Throwable caught) {
                    systeme.stopLoading();
                    response.setStatus (RPCResponse.STATUS_FAILURE);
                    processResponse (requestId, response);
                }
                public void onSuccess (Contact result) {
                    ListGridRecord[] list = new ListGridRecord[1];
                    list = new ListGridRecord[1];
                    list[0] = new ListGridRecord();
                                        
                    response.setData(list);
                    processResponse(requestId, response);
                    String organisationId = "-1";
                    if (systeme.getOrganisationUI().getForm().getItem("organisation_form_id") != null) {
                        if (systeme.getOrganisationUI().getForm().getItem("organisation_form_id").getValue() != null) {
                            try {
                                organisationId = ((Integer)systeme.getOrganisationUI().getForm().getItem("organisation_form_id").getValue()).toString();
                            }
                            catch(Exception e) {
                                organisationId = (String)systeme.getOrganisationUI().getForm().getItem("organisation_form_id").getValue();
                            }
                        }
                    }
                    //systeme.getOrganisationUI().getContactMiniListe().invalidateCache();
                    //systeme.getOrganisationUI().getContactMiniListe().fetchData(new Criteria("organisationId",organisationId));
                    systeme.getAppBar().selectTab("ORGANISATIONS");
                    SC.say("Item sauvegardé !");
                    
                }
            });
        }
        else {
            systeme.stopLoading();
            SC.say("Information obligatoire non complétée!");
        }
    }

    @Override
    protected void executeRemove (final String requestId, final DSRequest request, final DSResponse response) {
         //@TODO : fdsa
    }

}
